/*!
    \file    main.c
    \brief   led spark with systick

    \version 2021-03-25, V1.0.0, firmware for GD32W51x
*/

/*
    Copyright (c) 2021, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "gd32w51x.h"
#include "systick.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "rtthread.h"

#include "DevicesDelay.h"
#include "DevicesEEPROM.h"
#include "DevicesIIC.h"
#include "DevicesQueue.h"
#include "DevicesMem.h"
#include "DevicesUart.h"
#include "DevicesBMI055.h"
#include "DevicesLed.h"
#include "DevicesKey.h"
#include "DevicesTime.h"
#include "DevicesTimer.h"
#include "DevicesSoftTimer.h"
#include "DevicesADC.h"
#include "DevicesIFRP.h"
#include "DevicesSDCard.h"
#include "DevicesPower.h"
#include "DevicesTSI.h"
#include "DevicesRandom.h"
#include "DevicesCRC.h"
#include "DevicesWatchDog.h"
#include "DevicesSPI.h"
#include "DevicesIIS.h"
#include "DevicesMemPool.h"
#include "lcd_driver.h"
#include "main.h"

#include "UserTask.h"


/*!
    \brief      switch system clock to 180M by IRC16M
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void switch_system_clock_to_180m_irc16m(void)
{
    uint32_t timeout = 0U;
    uint32_t stab_flag = 0U;

    rcu_system_clock_source_config(RCU_CKSYSSRC_IRC16M);
    rcu_deinit();
    /* enable IRC16M */
    RCU_CTL |= RCU_CTL_IRC16MEN;

    /* wait until IRC16M is stable or the startup time is longer than IRC16M_STARTUP_TIMEOUT */
    do{
        timeout++;
        stab_flag = (RCU_CTL & RCU_CTL_IRC16MSTB);
    }while((0U == stab_flag) && (IRC16M_STARTUP_TIMEOUT != timeout));

    /* if fail */
    if(0U == (RCU_CTL & RCU_CTL_IRC16MSTB))
    {
        while(1);
    }

    RCU_APB1EN |= RCU_APB1EN_PMUEN;
    PMU_CTL0 |= PMU_CTL0_LDOVS;

    /* IRC16M is stable */
    /* AHB = SYSCLK */
    RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
    /* APB2 = AHB/2 */
    RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
    /* APB1 = AHB/4 */
    RCU_CFG0 |= RCU_APB1_CKAHB_DIV4;

    /* configure the main PLL, PSC = 16, PLL_N = 360, PLL_P = 2 */
    RCU_PLL = (16U | (360U << 6U) | (((2U >> 1U) - 1U) << 16U) |
                   (RCU_PLLSRC_IRC16M) );

    /* enable PLL */
    RCU_CTL |= RCU_CTL_PLLEN;

    /* wait until PLL is stable */
    while(0U == (RCU_CTL & RCU_CTL_PLLSTB));

    /* select PLL as system clock */
    RCU_CFG0 &= ~RCU_CFG0_SCS;
    RCU_CFG0 |= RCU_CKSYSSRC_PLLP;

    /* wait until PLL is selected as system clock */
    while(0U == (RCU_CFG0 & RCU_SCSS_PLLP));
}

void vUserSystemInit(void)
{
    vDelayInit();

    vRandHardInit();

    vHardCRCInit();

    vMemInit();
    vMemPoolInit();
    enumQueueInit();

    vLedInit();

    vKeyInit();

    vTSIInit();

    vEEPROMInit();

    vIFRPInit();

    lcd_init();

    vSDCardInit();

    vUart0Init();
    vUart1Init();

    // vPowerInit();

    // extern void vUsbInit(void);
    // vUsbInit();

    cTimer5Init();
    cTimer15Init();
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    /* enable icache */
    icache_enable();
    // /* configure systick */
    // systick_config();
    SystemCoreClockUpdate();
    printf("SystemCoreClock: %d\r\n", (int32_t)SystemCoreClock);

    /* set the priority group */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
    /* enable the peripherals clock */
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);

    /* configure the systick handler priority */
    nvic_irq_enable(SysTick_IRQn, 15, 0);

    extern void vTaskInit(void *pvParameters);
    if((g_TaskInitHand = rt_thread_create("TaskInit", vTaskInit, RT_NULL, 1024, 1, 20)) != NULL)
        rt_thread_startup(g_TaskInitHand);
}


int fputc(int ch, FILE *f)
{
    // /* Transmit Data */
    while((RESET == usart_flag_get(USART2, USART_FLAG_TBE)) && (USART_CTL0(USART2) & USART_CTL0_UEN) && (USART_CTL0(USART2) & USART_TRANSMIT_ENABLE));
    USART_TDATA(USART2) = ch;

    return ch;
}
